package com.services.impl;

import com.beans.Photo;
import com.commons.util.StringUtil;
import com.data.mapper.BaseMapper;
import com.data.mapper.PhotoMapper;
import com.services.PhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author jiangwei
 * @since 18/1/11
 */
@Service
public class PhotoServiceImpl extends BaseServiceImpl<Photo> implements PhotoService {

    @Autowired
    private PhotoMapper photoMapper;

    @Override
    public BaseMapper<Photo> getMapper(){
        return photoMapper;
    }

    /***
     * 操作图片
     * @param relId
     * @param photo
     * @param imgFileIds
     * @param imgFileDes
     * @param imgFileHXIds
     * @param imgFileHXDes
     */
    public void photo(PhotoService photoService, Long relId,Photo photo,
                      String[] imgFileIds, String[] imgFileDes,
                      String[] imgFileHXIds, String[] imgFileHXDes){
        //删除之前的图片
        photo.setRelationId(relId);
        photoService.delete(photo);

        savePhoto(imgFileIds,imgFileDes,photo,5,photoService);
        savePhoto(imgFileHXIds,imgFileHXDes,photo,1,photoService);
    }


    /***
     * 新房源图片
     * @param photoService
     * @param relId
     * @param photo
     * @param imgFileIds
     * @param imgFileDes
     * @param imgFileIds1
     * @param imgFileDes1
     * @param imgFileIds2
     * @param imgFileDes2
     * @param imgFileIds3
     * @param imgFileDes3
     */
    public void photoOfnewProject(PhotoService photoService, Long relId,Photo photo,
                                  String[] imgFileIds, String[] imgFileDes,
                                  String[] imgFileIds1, String[] imgFileDes1,
                                  String[] imgFileIds2, String[] imgFileDes2,
                                  String[] imgFileIds3, String[] imgFileDes3){
        //删除之前的图片
        photo.setRelationId(relId);
        photoService.delete(photo);

        savePhoto(imgFileIds,imgFileDes,photo,1,photoService);
        savePhoto(imgFileIds1,imgFileDes1,photo,2,photoService);
        savePhoto(imgFileIds2,imgFileDes2,photo,3,photoService);
        savePhoto(imgFileIds3,imgFileDes3,photo,4,photoService);

    }

    /***
     * 操作图片
     * @param relId
     * @param photo
     * @param imgFileIds
     * @param imgFileDes
     */
    public void contract(PhotoService photoService, Long relId,Photo photo,
                      String[] imgFileIds, String[] imgFileDes){
        //删除之前的图片
        photo.setRelationId(relId);
        photoService.delete(photo);

        savePhoto(imgFileIds,imgFileDes,photo,5,photoService);
    }


    /***
     * 保存图片
     * @param ids
     * @param des
     * @param photo
     */
    private void savePhoto(String[] ids, String[] des,Photo photo, Integer category, PhotoService photoService){
        if(!StringUtil.isEmpty(ids)){
            if(ids.length > 0){
                //循环保存图片
                for (int i = 0; i < ids.length; i++) {
                    if(!ids[i].isEmpty()){
                        photo.setPhotoUrl(ids[i]);
                        photo.setPhotoSource(ids[i]);
                        photo.setPhotoDesc(des[i]);
                        photo.setCategory(category);

                        photoService.add(photo);
                    }
                }
            }
        }
    }

    @Override
    public void updateQUrl(Photo photo) {
        photoMapper.updateQUrl(photo);
    }
}
